<?php
// +----------------------------------------------------------------------
// | mxAdmin
// +----------------------------------------------------------------------
// | 版权所有 2020~2050 福州目雪科技有限公司 [ http://www.muxue.com.cn ]
// +----------------------------------------------------------------------
// | 演示地址: http://demo.muxue.com.cn
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/muxue2020/mxAdmin
// +----------------------------------------------------------------------
// | Author: 明仔 <350656405@qq.com>    微信号：zlmlovem
// +----------------------------------------------------------------------

// user应用公共文件
if (!function_exists('getStoreId')) {
    /**
     * 获取商家ID
     * @return mixed
     */
    function getStoreId()
    {
        $data = session('user_info.store_id');
        return $data;
    }
}

if (!function_exists('getUserId')) {
    /**
     * 获取用户ID
     * @return mixed
     */
    function getUserId()
    {
        $data = session('user_info.user_id');
        return $data;
    }
}

if (!function_exists('getMenuData')) {
    /**
     * 获取菜单数据
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getMenuData()
    {
        if (session('user_info.is_admin') == 1) {
            $where = ['isnav' => 1, 'status' => 1];
        } else {
            $groupid = \app\user\model\UserAuthGroupAccess::where('uid', getUserId())->column('group_id');
            $authid = implode(',', \app\user\model\UserAuthGroup::where('id', 'in', $groupid)->column('rules'));
            $arr = explode(',', $authid); //explode()以逗号为分割，变成一个新的数组
            $arr = array_unique($arr); //array_unique()函数移除数组中的重复的值，并返回结果数组
            $data = implode(',', $arr); //implode() 函数返回由数组元素组合成的字符串
            $where = [
                ['id', 'in', $data],
                ['isnav', '=', 1],
                ['status', '=', 1],
            ];
        }
        $menulist = \app\user\model\UserAuthRule::where($where)->order(['weight', 'id'])->select()->toArray();
        return $menulist;
    }
}
